<?php
/**
 * ShopEx licence
 *
 * @copyright  Copyright (c) 2005-2010 ShopEx Technologies Inc. (http://www.shopex.cn)
 * @license  http://ecos.shopex.cn/ ShopEx License
 */

class importexport_tasks_runexport extends base_task_abstract implements base_interface_task{

    public function exec($params=null){
        $taskModel = app::get('importexport')->model('task'); 
        //执行队列，更新队列状态
        $taskModel->update(array('status'=>1),array('key'=>$params['key'])); 

        //连接导出文件上传的服务器
        $policyObj = kernel::single('importexport_policy');
        $ret = $policyObj->connect();
        if ( $ret !== true ){
            $taskModel->update( array('status'=>3,'complete_date'=>time(),'message'=>$ret), array('key'=>$params['key']) );exit;
        }

        //实例化导出数据类
        $dataObj = kernel::single('importexport_data_object',$params['model']);
        //实例化导出文件类型类
        $filetypeObj = kernel::single('importexport_type_'.$params['filetype']);

        $remote_file_name = $policyObj->create_remote_file($params);
        if( !$policyObj->create_local_file() ){
            $msg = app::get('importexport')->_('本地文件创建失败，请检查/tmp文件夹权限'); 
            $taskModel->update( array('status'=>3,'complete_date'=>time(),'message'=>$msg), array('key'=>$params['key']) );exit;
        }

        //加入文件头部数据
        $fileHeader = $filetypeObj->fileHeader();
        if( $fileHeader )
        {
            $policyObj->write_local_file($fileHeader);
            $policyObj->push();
        }
        if(isset($params['filter']['filter'])){
                //$params['filter'] = array_merge($params['filter'],$params['filter']['filter']);
                if($params['model']=='b2c_mdl_goods' && isset($params['filter']['filter']['cat_id'])){
                        $curcatid = $params['filter']['filter']['cat_id'];
                        $catids = app::get('b2c')->model('goods_cat')->getChildren($curcatid);
                        $catids[] =$curcatid;
                        
                        //$params['filter']['cat_id'] = $catids;
                        $goodslist = app::get('b2c')->model('goods')->parent_getList('goods_id',array('cat_id'=>$catids));
                        $goodsids = array();
                        foreach ($goodslist as $g){
                               $goodsids[] = $g['goods_id']; 
                        }
                        $filter['goods_id'] = $goodsids;
                }else{
                        $params['filter'] = array_merge($params['filter'],$params['filter']['filter']);
                        $filter = $dataObj->getIdFilter($params['filter']);
                }
        }else{
                $filter = $dataObj->getIdFilter($params['filter']);
        }
        //2017-7-21  by jeff,如果 $filter['login_account']是数组的话，会致数据集获取失败
        //解决过程：
        //1,发现下面的$dataObj->fgetlist为空,进而跟踪到b2c_mdl_analysis_member::getList方法,
        //将sql语句用logger::info()方法打出到文件中，发现sql语句中有like '%Arr%'字样,
        //2,猜测可能是二开导致，在config.php文件中注释掉二开目录，说明是二开导致
        //3，再次观察打入日志中的sql语句，发现没有like字句，确认原因是$filter变量中多出了login_account参数导致
        //最终分析下来原因：二开目录中importexport_data_object::getIdFilter方法注释掉了头几行代码，导致问题
        //最初的程序员因为什么注释掉，也没写注释，这是个大坑，
        if(is_array($filter['login_account'])) unset($filter['login_account']);

        //导出数据写到本地文件
        $offset = 0;
        while( $listFlag = $dataObj->fgetlist($data,$filter,$offset) ){
            $offset++;
            $rs = $filetypeObj->arrToExportType($data);
            if( !$policyObj->write_local_file($rs) ){
                $msg = app::get('importexport')->_('本地写入文件失败，请检查/tmp文件夹权限'); 
                $taskModel->update( array('status'=>3,'complete_date'=>time(),'message'=>$msg), array('key'=>$params['key']) );exit;
            }

            //本地文件上传到远程
            if( !$policyObj->push() ){
                $msg = app::get('importexport')->_('文件上传失败'); 
                $taskModel->update( array('status'=>3,'complete_date'=>time(),'message'=>$msg), array('key'=>$params['key']) );exit;
            }
        }

        //加入文件尾部数据
        $fileFoot = $filetypeObj->fileFoot();
        if( $fileFoot )
        {
            $policyObj->write_local_file($fileFoot);
            $policyObj->push();
        }

        $is_forma=true;//文件大小格式化 B kB MB
        $file_size = $policyObj->size_local_file($is_forma);
        //导出结束
        $taskModel->update( array('status'=>2,'complete_date'=>time(),'message'=>$file_size), array('key'=>$params['key']) );

        $policyObj->close_local_file();
        return true;
    }

}
